#include "MainScreen.h"

using namespace cocos2d;
using namespace std;

enum eSpriteTags {OpenFileLable, EditableSprite};

bool CMainScreen::init()
{
	if ( !CCLayer::init() )
	{
		return false;
	}
	m_pImageLoaded = false;
	m_nZOrder = 0;
	setTouchEnabled(true);
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	cocos2d::CCMenuItemImage* pExit = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(CMainScreen::ExitPressed));
	pExit->setPosition(ccp(size.width - (size.width * (float)0.05), size.width * (float)0.05));

	CCMenu* pMenu = CCMenu::create(pExit, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, ++m_nZOrder);

	m_pDebugDraw = DebugDraw::create();
	this->addChild(m_pDebugDraw);

	CCLabelTTF* label = CCLabelTTF::create("Choose an image...", "Arial", 20);
	CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(CMainScreen::OpenFileCallBack));

	CCMenu* pOpenFileMenu =CCMenu::create(pMenuItem, NULL);
	CCSize s = CCDirector::sharedDirector()->getWinSize();
	pOpenFileMenu->setPosition( CCPointZero );
	pMenuItem->setPosition( CCPointMake( s.width/2,s.height/2) );

	addChild(pOpenFileMenu, 1,OpenFileLable);



	CCLabelTTF* pDoneLable = CCLabelTTF::create("Done", "Arial", 20);
	CCMenuItemLabel* pDoneMenuItem = CCMenuItemLabel::create(pDoneLable, this, menu_selector(CMainScreen::ProcessOutput));

	CCMenu* pDoneMenu =CCMenu::create(pDoneMenuItem, NULL);
	pDoneMenu->setPosition( CCPointZero );
	pDoneMenuItem->setPosition(ccp(size.width/2, size.height-size.height/10));

	addChild(pDoneMenu, 1,OpenFileLable);


	return true;
}

cocos2d::CCScene* CMainScreen::scene()
{
	CCScene *pScene = CCScene::create();
	CMainScreen* pLayer = CMainScreen::create();
	pScene->addChild(pLayer);

	return pScene;
}

void CMainScreen::ExitPressed( CCObject* pSender )
{
	CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}


void CMainScreen::OpenFileCallBack(CCObject* pSender)
{
	CCFileUtils::sharedFileUtils()->setResourceDirectory("../images");
	const char* pzsFilePath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Cloud.png");
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCSprite* pSprite = CCSprite::create(pzsFilePath);
	pSprite->setPosition( ccp(size.width/2, size.height/2) );
	this->addChild(pSprite, ++m_nZOrder,EditableSprite);
	removeChild(getChildByTag(OpenFileLable),true);
	m_pImageLoaded = true;
}



void CMainScreen::ccTouchesBegan(CCSet* touches, CCEvent* event)
{
	CCSetIterator it;
	CCTouch* touch;

	for( it = touches->begin(); it != touches->end(); it++) 
	{
		touch = (CCTouch*)(*it);

		if(!touch)
			break;

		CCPoint location = touch->getLocation();
		if (m_pImageLoaded)
		{
			DrawPoint(location);
		}
	}
}

void CMainScreen::ccTouchesMoved(CCSet* touches, CCEvent* event)
{
	CCSetIterator it;
	CCTouch* touch;

	for( it = touches->begin(); it != touches->end(); it++) 
	{
		touch = (CCTouch*)(*it);

		if(!touch)
			break;

		CCPoint location = touch->getLocation();
		if (m_pImageLoaded)
		{
			//Commented out as cocos doesn't support canvas polygons
			//DrawPoint(location);
		}
	}
}


void CMainScreen::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
	//Add a new body/atlas sprite at the touched location
	CCSetIterator it;
	CCTouch* touch;

	for( it = touches->begin(); it != touches->end(); it++) 
	{
		touch = (CCTouch*)(*it);

		if(!touch)
			break;
		CCPoint location = touch->getLocation();
	}
}


void CMainScreen::DrawPoint(CCPoint point)
{
	int count = m_Points.size();
	CCPoint prev;
	if (count != NULL)
	{
		prev = m_Points[count-1];
	}

	m_Points.push_back(point);
	count++;

	m_pDebugDraw->appendPoint(point);

	if (count < 2) return;

	if (!CCPoint::CCPointEqualToPoint(prev, point))
	{
		m_pDebugDraw->appendLine(prev, point);
	}
}


void CMainScreen::ProcessOutput(CCObject* pSender)
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	//make a point to go from. Make it to be the center of a sprite image.
	
	CCPoint startPoint = CCPointMake(size.width/2,size.height/2);
	int i;

	i=5;

	i = 10+-2;
	//CCLog("touch, x = %f, y = %f", 5, 6);
	CCLog("CCPoint startPoint = CCPointMake(size.width/2,size.height/2); /* where size is the size or your sprite*/");
	CCLog("int num =%d;",m_Points.size());
	CCLog("cpVect verts[] = {");
	
	for (size_t i = 0; i<m_Points.size(); i++)
	{
		if (i != m_Points.size()-1)
		{
			CCLog("cpv(startPoint.x+%f,startPoint.y+%f),",m_Points[i].x-startPoint.x,m_Points[i].y-startPoint.y);
		}
		else
		{
			CCLog("cpv(startPoint.x+%f,startPoint.y+%f)",m_Points[i].x-startPoint.x,m_Points[i].y-startPoint.y);
		}

	}
	CCLog("};");
}